package com.ovo.rxandroiddemo.utils;

import android.graphics.Color;
import android.widget.TextView;

import com.lljjcoder.citypickerview.widget.CityPicker;
import com.ovo.rxandroiddemo.R;
import com.ovo.rxandroiddemo.app.BaseApplication;

/**
 * 城市三级联动选择器
 */
public class CityPickerUtils {
    private static CityPickerUtils utils = null;

    public static CityPickerUtils getInstance(){
        if (utils == null){
            synchronized (CityPickerUtils.class){
                if (utils == null){
                    utils = new CityPickerUtils();
                }
            }
        }
        return utils;
    }

    /**
     * 获取地区
     **/
    public void selectAddress(TextView text, String addr) {
        String province = "广东省";
        String city = "深圳市";
        String district = "宝安区";
        if (!StringUtils.isEmpty(addr)){
            String []addrArray = addr.split("-");
            if (addrArray.length >= 3){
                province = addrArray[0];
                city = addrArray[1];
                district = addrArray[2];
            }
        }
        CityPicker cityPicker = new CityPicker.Builder(BaseApplication.context)
                .textSize(14)
                .title("地址选择")
                .titleBackgroundColor("#FFFFFF")
                .textColor(BaseApplication.activity.getResources().getColor(R.color.city_color))
//                .titleTextColor("#696969")
                .confirTextColor("#696969")
                .cancelTextColor("#696969")
                .province(province)
                .city(city)
                .district(district)
                .textColor(Color.parseColor("#333333"))
                .provinceCyclic(true)
                .cityCyclic(false)
                .districtCyclic(false)
                .visibleItemsCount(7)
                .itemPadding(10)
                .onlyShowProvinceAndCity(false)
                .build();
        cityPicker.show();
        //监听方法，获取选择结果
        cityPicker.setOnCityItemClickListener(new CityPicker.OnCityItemClickListener() {
            @Override
            public void onSelected(String... citySelected) {
                //省份
                String province = citySelected[0];
                //城市
                String city = citySelected[1];
                //区县（如果设定了两级联动，那么该项返回空）
                String district = citySelected[2];
                //邮编
                String code = citySelected[3];
                //为TextView赋值
                province = province.trim();
                city = city.trim();
                district = district.trim();
                text.setText(province + "-" + city + "-" + district);
            }
        });
    }
}
